function perm(array) {
    let result = []
    if(array.length === 0) return []
    if(array.length === 1) return [array[0]]
    let count = 0
    function fix(x){
        if(x === array.length-1){
            // count++
            for(let i = 0;i < array.length;i++){
                for(let j = i+1;j < array.length;j++){
                    if(i-j !== array[i]-array[j] && i-j !== array[j]-array[i]){
                        count++
                    }
                }
            }
        }else{
            let seen = new Set()
            for(let i = x;i < array.length;i++){
                if(seen.has(array[i])){
                    continue
                }
                seen.add(array[i])
                let temp = array[x]
                array[x] = array[i]
                array[i] = temp
                fix(x+1)
                temp = array[x]
                array[x] = array[i]
                array[i] = temp
            }
        }
    }
    fix(0)
    return count
}
console.log(perm([0,1,2,3,4,5,6,7]));